var port = null; 
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
  	console.log(4455);
     if (request.type == "launch"){
         connectToNativeHost(request.message, sendResponse);
     }     
	return true;
});


//onNativeDisconnect
function onDisconnected()
{
	console.log(chrome.runtime.lastError);
	console.log('disconnected from native app.');
	port = null;
}

function onNativeMessage(message, sendResponse) {    
    console.log('recieved message from native app: ' + JSON.stringify(message));
    sendResponse(message);
}

//connect to native host and get the communicatetion port
function connectToNativeHost(msg,sendResponse)
{
	var nativeHostName = "com.mosai.screenshare";
	console.log(msg);
 	port = chrome.runtime.connectNative(nativeHostName);
 	port.onMessage.addListener(function (message) {
 	    onNativeMessage(message, sendResponse);
    });
 	//port.onMessage.addListener(onNativeMessage);
	port.onDisconnect.addListener(onDisconnected);
	port.postMessage(msg);	
 } 